package sfeir.ergosum;

import com.google.zxing.BarcodeFormat;
import com.google.zxing.MultiFormatWriter;
import com.google.zxing.WriterException;
import com.google.zxing.common.ByteMatrix;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.content.DialogInterface.OnCancelListener;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.Gravity;
import android.widget.ImageView;
import android.widget.LinearLayout;

public class QrifiedContactUrlActivity extends Activity {

    private ProgressDialog mProgressDialog;
    private ImageView mQrCodeView;

    public final Handler mHandler = new Handler() {
        @Override
        public void handleMessage(Message message) {
            switch (message.what) {
            case R.id.encode_succeeded:
                mProgressDialog.dismiss();
                mProgressDialog = null;
                Bitmap image = (Bitmap) message.obj;
                mQrCodeView.setImageBitmap(image);
                break;
            case R.id.encode_failed:
                if (mProgressDialog != null) {
                    mProgressDialog.dismiss();
                    mProgressDialog = null;
                }
                new AlertDialog.Builder(QrifiedContactUrlActivity.this)
                        .setMessage("Encoding failed").show();
                break;
            }
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        LinearLayout layout = new LinearLayout(this);
        layout.setGravity(Gravity.CENTER);
        mQrCodeView = new ImageView(this);
        mQrCodeView.setMinimumHeight(200);
        mQrCodeView.setMinimumWidth(200);

        layout.addView(mQrCodeView);

        setContentView(layout);

        mProgressDialog = ProgressDialog.show(QrifiedContactUrlActivity.this,
                null, "Generating QR image...", true, true,
                new OnCancelListener() {
                    public void onCancel(DialogInterface dialog) {
                        finish();
                    }
                });
        Thread encodeThread = new EncodeThread(getIntent().getStringExtra(
                "CONTACT_URL"), mHandler, 200);
        encodeThread.start();
    }

    private static final class EncodeThread extends Thread {

        private static final String TAG = "EncodeThread";

        private final String mContents;
        private final Handler mHandler;
        private final int mPixelResolution;

        EncodeThread(String contents, Handler handler, int pixelResolution) {
            mContents = contents;
            mHandler = handler;
            mPixelResolution = pixelResolution;
        }

        public void run() {
            try {
                ByteMatrix result = new MultiFormatWriter().encode(mContents,
                        BarcodeFormat.QR_CODE, mPixelResolution,
                        mPixelResolution);
                int width = result.width();
                int height = result.height();
                byte[][] array = result.getArray();
                int[] pixels = new int[width * height];
                for (int y = 0; y < height; y++) {
                    for (int x = 0; x < width; x++) {
                        int grey = array[y][x] & 0xff;
                        // pixels[y * width + x] = (0xff << 24) | (grey << 16) |
                        // (grey << 8) | grey;
                        pixels[y * width + x] = 0xff000000 | (0x00010101 * grey);
                    }
                }

                Bitmap bitmap = Bitmap.createBitmap(width, height,
                        Bitmap.Config.ARGB_8888);
                bitmap.setPixels(pixels, 0, width, 0, 0, width, height);
                Message message = Message.obtain(mHandler,
                        R.id.encode_succeeded);
                message.obj = bitmap;
                message.sendToTarget();
            } catch (WriterException e) {
                Log.e(TAG, e.toString());
                Message message = Message.obtain(mHandler, R.id.encode_failed);
                message.sendToTarget();
            } catch (IllegalArgumentException e) {
                Log.e(TAG, e.toString());
                Message message = Message.obtain(mHandler, R.id.encode_failed);
                message.sendToTarget();
            }
        }
    }

}
